//
//  ViewController.m
//  送礼
//
//  Created by 华惠友 on 2017/9/19.
//  Copyright © 2017年 华惠友. All rights reserved.
//

#import "ViewController.h"
#import "ZBGiftView.h"
#import "ZBBottomToolsView.h"



// 点赞
#import "ZBApplauseAnimationView.h"

#import "ZBGiftModel.h"
#import "ZBPresentView.h"

#import "ZBGiftOperationManager.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

static const CGFloat kBottomToolViewHeight = 64;

@interface ViewController ()
@property (nonatomic, strong) ZBGiftView *giftView;

@property (nonatomic ,strong) ZBBottomToolsView *bottomTool;


@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSMutableArray *titleArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];

    [self.view addSubview:self.bottomTool];
    
}

- (void)makeAnimationTag:(NSInteger)tag {
    // 礼物视图消失
//    [self.giftView removeFromSuperview];
//    self.giftView = nil;
//    
    static int giftCount = 1;
    // 礼物模型
    NSUInteger userID = arc4random()%2;
    ZBGiftModel *giftModel = [ZBGiftModel modelWithuserID:userID Sender:[NSString stringWithFormat:@"%lu",(unsigned long)userID] headName:@"luffy.jpg" giftImageName:self.imageArray[tag-1000] giftName:self.titleArray[tag-1000]];

    giftCount ++;
//    NSLog(@"%lu",(unsigned long)userID);
    ZBGiftOperationManager *manager = [ZBGiftOperationManager shareManager];
    manager.parentView = self.view;
    [manager animationWithGiftIndex:tag giftModel:giftModel finishedBlock:^(BOOL isFinished, NSInteger giftCount) {
//        NSLog(@"动画结束");
        
//        [self popShowGiftView];

    }];
    
}

// 点击礼物界面之外的点礼物界面消失
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [allTouches anyObject];
    CGPoint point = [touch locationInView:self.view];
    if (point.y < CGRectGetMinY(_giftView.frame)) {
        if (_giftView) {
            [self.giftView removeFromSuperview];
            self.giftView = nil;
            [self bottomToolShow];
        }
    }
    
}

// 出现礼物
- (void)popShowGiftView {
    [UIView animateWithDuration:0.2 animations:^{
        [self.view addSubview:self.giftView];
    }];
}

// 底部工具栏出现
- (void)bottomToolShow {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.values = @[[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0,SCREEN_HEIGHT+kBottomToolViewHeight/2.0)],
                         [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0,SCREEN_HEIGHT-kBottomToolViewHeight/2.0)]];
    animation.keyTimes = @[@0.0, @1.0];
    animation.duration = 0.5;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    [self.bottomTool.layer addAnimation:animation forKey:@"keyAnimation"];
}

// 底部工具栏消失
- (void)bottomToolHide {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.values = @[[NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0,SCREEN_HEIGHT-kBottomToolViewHeight/2.0)],
                         [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2.0,SCREEN_HEIGHT+kBottomToolViewHeight/2.0)]];
    animation.keyTimes = @[@0.0, @1.0];
    animation.duration = 0.5;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [self.bottomTool.layer addAnimation:animation forKey:@"keyAnimation"];
    
    
    [self performSelector:@selector(popShowGiftView) withObject:nil afterDelay:0.5];
}


- (ZBBottomToolsView *)bottomTool{
    
    if (!_bottomTool) {
        _bottomTool = [[ZBBottomToolsView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - kBottomToolViewHeight, SCREEN_WIDTH, kBottomToolViewHeight)];
        __weak typeof(self)weakSelf =self;
        [_bottomTool setButtonClick:^(NSInteger tag, UIButton *sender) {
            switch (tag) {
                case 100: {
                    //发送消息/弹幕
                
                   
                }
                    break;
                case 101: {
                    //礼物
                
                    [weakSelf bottomToolHide];
                }
                    
                    break;
                case 102: {
                    //显示分享面板
                
                }
                    
                    break;
                case 103: {
                    //  点赞
                    CAKeyframeAnimation *btnAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
                    btnAnimation.values = @[@(1.0),@(0.7),@(0.5),@(0.3),@(0.5),@(0.7),@(1.0), @(1.2), @(1.4), @(1.2), @(1.0)];
                    btnAnimation.keyTimes = @[@(0.0),@(0.1),@(0.2),@(0.3),@(0.4),@(0.5),@(0.6),@(0.7),@(0.8),@(0.9),@(1.0)];
                    btnAnimation.calculationMode = kCAAnimationLinear;
                    btnAnimation.duration = 0.3;
                    [sender.layer addAnimation:btnAnimation forKey:@"SHOW"];
                    [ZBApplauseAnimationView showTheApplauseInView:weakSelf.view belowView:sender];
                }
                default:
                    break;
            }
        }];
    }
    return _bottomTool;
}


- (ZBGiftView *)giftView {
    if (!_giftView) {
        _giftView = [[ZBGiftView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-300, self.view.frame.size.width, 300)];
        __weak typeof(self)weakSelf = self;
        _giftView.giftBlock = ^(NSInteger tag) {
//            NSLog(@"%ld",(long)tag);
            // 轨迹动画  帧动画  粒子动画
            [weakSelf makeAnimationTag:tag];
        };
    }
    return _giftView;
}

//礼物图片
- (NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
        for (NSInteger i = 0; i < 24; i++) {
            NSString *imagePath=[NSString stringWithFormat:@"yipitiezhi0%zd",i + 1];
            [_imageArray addObject:imagePath];
        }
    }
    return _imageArray;
}

- (NSMutableArray *)titleArray {
    if (!_titleArray) {
        _titleArray = [NSMutableArray arrayWithObjects:@"轨迹轮船", @"粒子花花" , @"帧动画钻石" , @"重力❤️" , @"UIView法拉利" , @"镜子" , @"花圈" , @"围巾" , @"领结" , @"气球" , @"麦克风" , @"小草" , @"帽子" , @"太阳" , @"小马", @"兔帽", @"领结" , @"气球" , @"麦克风" , @"小草" , @"帽子" , @"太阳" , @"小马", @"兔帽", nil];
    }
    return _titleArray;
}


@end
